<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<uni-segmented-control :current="current" :values="items" :style-type="styleType"
				:active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<view class="content">
			<view v-if="current === 0">
				<uni-card :is-shadow="false" is-full :border="false" v-for="item in Shelves" class="list" :key="item.id">
					<view class="statusList">
						<navigator url="/pages/index/activityDetail/activityDetail" hover-class="none">
							<view class="uni-h6">{{item.title}}</view>
							<view>{{item.time}}</view>
						</navigator>
						<view class="example-body">
							<view class="tag-view">
								<uni-tag type="primary" text="下架" @click="setType(item)" />
							</view>
						</view>
					</view>
				</uni-card>
			</view>
			<view v-if="current === 1">
				<uni-card :is-shadow="false" is-full :border="false" v-for="item in NoShelves" class="list" :key="item.id">
					<view class="statusList">
						<navigator url="/pages/index/activityDetail/activityDetail" hover-class="none">
							<view class="uni-h6">{{item.title}}</view>
							<view>{{item.time}}</view>
						</navigator>
						<view class="example-body">
							<view class="tag-view">
								<view :class="colorIndex==0?'review':colorIndex==1?'pass':'fail'">
									{{item.status == 0? ststus[0]:item.status == 1? ststus[1]:ststus[2]}}
								</view>
							</view>
							<view class="tag-view">
								<uni-tag v-if="item.status == 1" type="primary" text="上架" @click="shelves(item)" />
								<uni-tag v-if="item.status != 1" type="primary" text="删除" @click="remove(item)" />
							</view>
						</view>
					</view>
				</uni-card>
			</view>
		</view>
		<button class="button" @click="release">+发布活动</button>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				tokenId:"eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjkwMmIzMzU5LTFhYjEtNDdkNy1iZmFhLThiOThkOTBmZTA3NiIsInVzZXJuYW1lIjoiYWRtaW4ifQ.usn4WzRNgsJC5gm2JFnRO_ZAgwsPLYqNZ2H6pKJvZ9DPZ5PxAQAJTBQO81co9AZmmyII_Tf_cwOMWB1EV4yrRw",
				items: ['已上架', '未上架'],
				styles: [{
						value: 'button',
						text: '按钮',
						checked: true
					},
					{
						value: 'text',
						text: '文字'
					}
				],
				colors: ['#FB8E0B', '#4cd964', '#dd524d'],
				current: 0,
				colorIndex: 0,
				activeColor: '#007aff',
				styleType: 'text',
				type: "default",
				ststus: ["审核中","已审核","审核失败"],
				// ststusIndex:0,
				Shelves:[],
				NoShelves:[]
			}
		},
		methods: {
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
				this.getShelves()
				this.getNoShelves()
			},
			styleChange(e) {
				if (this.styleType !== e.detail.value) {
					this.styleType = e.detail.value
				}
				this.getShelves()
				this.getNoShelves()
			},
			colorChange(e) {
				if (this.styleType !== e.detail.value) {
					console.log(e.detail.value);
					this.activeColor = e.detail.value
				}
				this.getShelves()
				this.getNoShelves()
			},
			//下架
			setType(e) {
				uni.request({
					url:"http://localhost:8080/advertising/purchase",
					method:'PUT',
					header: {
						Authorization:this.tokenId
					},
					data:{
						id:e.id,
						shelves:1
					},
				})
				this.getNoShelves()
				this.getNoShelves()
				this.current = 1
			},
			remove(item) {
				console.log(item)
				uni.request({
					url:"http://localhost:8080/advertising/purchase",
					method:'PUT',
					header: {
						Authorization:this.tokenId
					},
					data:{
						id:item.id,
						deleted:1,
					},
					success: (resp) => {
						console.log(resp)
						if(resp.data.code == 200){
							this.getNoShelves()
						}
					}
				})
			},
			release() {
				uni.navigateTo({
					url: '/pages/index/release/release',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			getShelves(){
				uni.request({
					url:"http://localhost:8080/advertising/purchase/list",
					method:'GET',
					header: {
						Authorization:this.tokenId
					},
					data:{
						shelves:0,
						deleted:0
					},
					success: (resp) => {
						if(resp.data.code == 200){
							this.Shelves = resp.data.rows
						}
					}
				})
			},
			getNoShelves(){
				uni.request({
					url:"http://localhost:8080/advertising/purchase/list",
					method:'GET',
					header: {
						Authorization:this.tokenId
					},
					data:{
						shelves:1,
						deleted:0
					},
					success: (resp) => {
						if(resp.data.code == 200){
							this.NoShelves = resp.data.rows
						}
					}
				})
			},
			//上架
			shelves(item){
				console.log(item)
				uni.request({
					url:"http://localhost:8080/advertising/purchase",
					method:'PUT',
					header: {
						Authorization:this.tokenId
					},
					data:{
						id:item.id,
						shelves:0
					},
				})
				this.getShelves()
				this.getShelves()
				this.current = 0
			}
		},
		onLoad() {
			this.getShelves()
			this.getNoShelves()
		}
	}
</script>



<style>
	page {
		background-color: #fff;
	}

	.tag-view {
		/* float: right; */
		margin: 10rpx 0;
	}

	.uni-h6 {
		font-size: 24rpx;
		color: #000;
		margin: 16rpx 0;
	}

	.list {
		/* height: 160rpx; */
		/* padding-bottom: 20rpx; */
		border-bottom: 2rpx solid #BBBBBB;

	}

	.statusList {
		display: flex;
		justify-content: space-between;
	}

	.button {
		width: 70%;
		background-color: #FB8E0B;
		color: #fff;
		position: absolute;
		left: 15%;
		bottom: 30rpx;
	}

	.review {
		color: #FB8E0B;
	}

	.pass {
		color: #81B337;
	}

	.fail {
		color: #dd524d;
	}
</style>